param(
    [Parameter(Mandatory=$true)]
    [string]$ResourcesDir,
    
    [Parameter(Mandatory=$true)]
    [string]$OutputFile
)

# Get all files in the Resources directory
$files = Get-ChildItem -Path $ResourcesDir -Recurse -File | Sort-Object -Property FullName

# Create SHA256 hasher
$sha256 = [System.Security.Cryptography.SHA256]::Create()

# Create memory stream to combine all file contents
$memoryStream = New-Object System.IO.MemoryStream

# Add each file's path and contents to the hash
foreach ($file in $files) {
    # Get relative path and convert to lowercase for consistent hashing
    $relativePath = $file.FullName.Substring($ResourcesDir.Length + 1).ToLowerInvariant()
    $pathBytes = [System.Text.Encoding]::UTF8.GetBytes("QuickLook.Plugin.MarkdownViewer.Resources.$relativePath".Replace("\", "/"))
    $memoryStream.Write($pathBytes, 0, $pathBytes.Length)
    
    # Add file contents
    $fileBytes = [System.IO.File]::ReadAllBytes($file.FullName)
    $memoryStream.Write($fileBytes, 0, $fileBytes.Length)
}

# Calculate final hash
$hashBytes = $sha256.ComputeHash($memoryStream.ToArray())
$hash = [BitConverter]::ToString($hashBytes).Replace("-", "").ToLowerInvariant()

# Generate C# code
$code = @"
// <auto-generated>
// This file was generated during build to indicate changes to embedded resources.
// </auto-generated>

namespace QuickLook.Plugin.MarkdownViewer
{
    internal static class EmbeddedResourcesHash
    {
        internal const string Hash = "$hash";
    }
}
"@

# Write to output file
[System.IO.File]::WriteAllText($OutputFile, $code)

# Cleanup
$memoryStream.Dispose()
$sha256.Dispose()
